<!DOCTYPE html>
<head>
    <meta charset="UTF-8">
    <title>兑换</title>
    <script type="text/javascript" src="/js/jquery.js"></script>
</head>
<body>
<form action="" id = "J_form">
    兑换方式:
    <div><input type="radio" name="type" value="0">到店自提
         <input type="radio" name="type" value ="1">快递
    </div>

    <div id="J_shop_address">
        <input type="hidden" id = "J_name" name = "name" value="">
        <input type="hidden" id="J_phone" name = "phone" value ="">
        <input type="hidden" id="J_address" name="address" value="" >

    </div>


    个人地址:
    <div >
      <ul id ="J_user_ddress">
      </ul>
    </div>

<div>
    <div id ="J_pic"></div>
    <div id ="J_title"></div>
    <div id ="J_points"></div>

</div>

    <buttom id="J_submit">确认兑换</buttom>
</form>

<script>
    $(function(){
      getShopAddress();
      getUserAddress();
      getProductDetail();

      //提交
      $("#J_submit").onclick(function(){
          var data = {uid:uid};
          var url = "/exchange/exchange";
          $.ajax({
              type: "POST",
              dataType: 'json',
              async: false,
              contentType: "application/json; charset=utf-8",
              data: JSON.stringify(data),
              url: url,
              success: function(res){
                  if(res.code = "200"){
                      var list = res.data;
                      if(list.length>0){
                          for (var i =0;i<list.length;i++){
                              if(list[i]['isdefault'] ==1){
                                  var shtml = '<div>'+list[i]['name']+'--'+list[i]['phone']+'</div><div>'+list[i].address+'</div>';
                                  $("#J_address").append(shtml);
                              }
                          }
                      }else{
                          var shtml = '<div>暂无地址</div>';
                          $("#J_address").append(shtml);
                      }
                  }else{
                      alert(res.msg);
                  }
              },
              error: function(e){

              },
              complete: function(e){

              }
          });

      });

    });


    /*获取店铺的地址*/
    function getShopAddress(){
        var id = "${uid}";
        var data = {uid:0};
        var url = "/address/get_address";
        $.ajax({
            type: "POST",
            dataType: 'json',
            async: false,
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify(data),
            url: url,
            success: function(res){
                if(res.code = "200"){
                    var list = res.data;
                    if(list.length>0){
                        for (var i =0;i<list.length;i++){
                            var shtml = '<li><div>'+list[i].address+'</div></li>';
                            $("#J_shop_address").append(shtml);
                        }
                    }
                }else{
                    alert(res.msg);
                }
            },
            error: function(e){

            },
            complete: function(e){

            }
        });
    }

    /*获取个人的地址*/
    function getUserAddress(){
        var uid = "${uid}";
        var data = {uid:uid};
        var url = "/address/get_address";
        $.ajax({
            type: "POST",
            dataType: 'json',
            async: false,
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify(data),
            url: url,
            success: function(res){
                if(res.code = "200"){
                    var list = res.data;
                    if(list.length>0){
                        for (var i =0;i<list.length;i++){
                            if(list[i]['isdefault'] ==1){
                                var shtml = '<div>'+list[i]['name']+'--'+list[i]['phone']+'</div><div>'+list[i].address+'</div>';
                                $("#J_address").append(shtml);
                            }
                        }
                    }else{
                        var shtml = '<div>暂无地址</div>';
                        $("#J_address").append(shtml);
                    }
                }else{
                    alert(res.msg);
                }
            },
            error: function(e){

            },
            complete: function(e){

            }
        });
    }


    /*获取产品详情*/
    function getProductDetail(){
        var id = "${id}";
        var data = {id:id};
        var url = "/product/product_detail";
        $.ajax({
            type: "POST",
            dataType: 'json',
            async: false,
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify(data),
            url: url,
            success: function(res){
                if(res.code = "200"){
                    var detail = res.data.product;
                    $("#J_pic").append("<img src='"+detail.pic+"'>");
                    $("#J_title").append(detail.title);
                    $("#J_points").append(detail.point);
                }else{
                    alert(res.msg);
                }
            },
            error: function(e){

            },
            complete: function(e){

            }
        });
    }


</script>
</body>
</html>
